<?php
/**
 * Created by PhpStorm.
 * User: Sojo
 * Date: 2017/6/20
 * Time: 19:34
 */

namespace App\Api\Service\Website\Models;

use Framework\BaseClass\Api\Model;
use \App\Eloquent\Ygt\AppVersion;

class AppInfo extends Model
{
    const PLATFORM_UNKNOWN = 0;
    const PLATFORM_ANDROID = 1;

    const PLATFORM_IOS = 2;

    const CURRENT_VERSION = 1;

    public function getAboutInfo($platform, $version)
    {
        $platform = $this->formatPlatform($platform);

        $appVersionInfo = Version::where([
            'platform' => $platform,
            'version'  => $version
        ])->first(['name', 'version', 'description']);

        if ($appVersionInfo) {
            $code_img = file_exists(public_path('/assets/api/images/share.png')) ? \Config::get('app.url') . '/assets/api/images/share.png' : '';
            $appVersionInfo->code_img = $code_img;
        }

        return $appVersionInfo;
    }

    public function getVersionUpdateInfo($platform, $currentVersion)
    {
//        //强制转换platform值
//        if(!in_array($platform,['ios','android'])){
//            if($platform == 1){
//                $platform = "android";
//            }
//            if($platform == 2){
//                $platform = "ios";
//            }
//        }
//
//        $platform = $this->formatPlatform($platform);

        $versionInfo = \App\Eloquent\Ygt\AppVersion::where([
            ['platform', '=', $platform],
            ['is_current_version', '=', self::CURRENT_VERSION],
            ['version', '>', $currentVersion]
        ])->first(['name', 'version', 'url', 'description', 'is_force_update']);

        if ($versionInfo) {
            $versionInfo->is_have_update = 1;
        } else {
            $versionInfo = ['is_have_update' => 0];
        }

        return $versionInfo;
    }

    private function formatPlatform($platform)
    {
        switch ($platform) {
            case 'android':
                $platform = self::PLATFORM_ANDROID;
                break;
            case 'ios':
                $platform = self::PLATFORM_IOS;
                break;
            default:
                $platform = self::PLATFORM_UNKNOWN;
        }

        return $platform;
    }
}